package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.correction.model.Crc_person;
import com.power.sifa.correction.service.Crc_personService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Crc_group_person;
import com.power.sifa.correction.model.pageInfo.Crc_group_personPageInfo;
import com.power.sifa.correction.api.ICrc_group_personFeign;
import com.power.sifa.correction.service.Crc_group_personService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



/**
 *   社区矫正分组内人员控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Crc_group_personController",tags={"社区矫正分组内人员"})
public class Crc_group_personController implements ICrc_group_personFeign {

    @Resource
    private Crc_group_personService crc_group_personService;

    @Resource
    private Crc_personService crc_personService;

    
    @Override
    @ApiOperation("查询社区矫正分组内人员分页列表")
    @SaCheckPermission("crc_group_person_list")
    public ResponseDTO list(Crc_group_personPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Crc_group_person> page = this.crc_group_personService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Crc_group_person crc_group_person : page) {
                crc_group_person.geneText();
                list.add(crc_group_person);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看社区矫正分组内人员")
    @SaCheckPermission("crc_group_person_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Crc_group_person crc_group_person  = crc_group_personService.getById(id);
        if (crc_group_person != null) {
            crc_group_person.geneText();
        }
        return ResponseDTO.succData(crc_group_person);
    }

    @Override
    @ApiOperation("删除社区矫正分组内人员")
    @SaCheckPermission("crc_group_person_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        crc_group_personService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改社区矫正分组内人员")
    @SaCheckPermission("crc_group_person_save")
    public ResponseDTO save(Crc_group_person crc_group_person) {
        Assert.notNull(crc_group_person, "crc_group_person不能为空");
        crc_group_personService.saveOrUpdate(crc_group_person);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除社区矫正分组内人员")
    @SaCheckPermission("crc_group_person_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        crc_group_personService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("crc_group_person_list")
    public void output(HttpServletResponse response, Crc_group_personPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "社区矫正分组内人员";
        String[] heads = {"grpid_perid","分组id","人员id"};
        String[] fields = {"id","grpid","perid"};
        Page<Crc_group_person> page = this.crc_group_personService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "社区矫正分组内人员";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"grpid_perid","分组id","人员id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","grpid","perid"};
        List<Crc_group_person> list = (List<Crc_group_person>)ExcelUtil.Import(fields, file, Crc_group_person.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.crc_group_personService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO deleteByPersonId(String personId, String groupId) {
        Assert.notNull(personId, "personId不能为空");
        Assert.notNull(groupId, "groupId不能为空");
        crc_group_personService.deleteByPersonId(personId,groupId);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<Map<String, List<Crc_person>>> personsByGroupIds(String groupIds) {
        Assert.notNull(groupIds, "groupIds不能为空");
        return ResponseDTO.succData(crc_personService.findPersonByGroupIds(Arrays.asList(groupIds.split(","))));
    }

}
